package com.nny.alarm;

import com.nny.alarm.alert.AlertActivity;
import com.nny.alarm.more.AlarmBriefUtil;
import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.Cmd;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Parcel;
import android.widget.RemoteViews;

public class NNYNotification
{
	Context mCtx;
	NotificationManager mNotificationMananger;
	static final int ID_MAIN = -1;
	AlarmBriefUtil mBrief;
	
	
	static NNYNotification mInstance;
	public synchronized static NNYNotification getInstance(Context ctx)
	{
		if(null == mInstance)
		{
			mInstance = new NNYNotification(ctx.getApplicationContext());
		}
		return mInstance;
	}
	
	private NNYNotification(Context ctx)
	{
		mCtx = ctx;
		mNotificationMananger = (NotificationManager)mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
		mBrief = AlarmBriefUtil.getInstance(ctx);
	}
	
	public void notify(int id, Notification notification)
	{
		mNotificationMananger.notify(id, notification);
	}
	
	public void cancel(int id)
	{
		mNotificationMananger.cancel(id);
	}
	
	public void cancelAll()
	{
		mNotificationMananger.cancelAll();
	}
	
	public void cancelMain()
	{
		cancel(ID_MAIN);
	}
	
	/**
	 * 这个是常驻通知栏的，点击后进入主页.
	 * <pre>
	 * 1.表示下个闹钟的时间
	 * </pre>
	 */
	public void notifyAlarm(Alarm alarm)
	{
		notify(ID_MAIN, createAlarmIntent(alarm));
	}
	
	/**
	 * 提示当前闹钟,这个是非常驻的，当前正在闹的闹钟。
	 * <pre>
	 * 1.点击直接推迟
	 * 2.推迟后进入闹钟页面
	 * </pre>
	 */
	public void notifyAlert(Alarm alarm)
	{
		notify(alarm.id, createAlertIntent(alarm));
	}
	
	/**
	 * 提示当前闹钟进入贪睡模式,这个是非常驻的，点击后进入闹钟页面
	 */
	public void notifySnoozed(Alarm alarm)
	{
		notify(alarm.id, createSnoozedIntent(alarm));
	}
	
	/**
	 * 提示当前闹钟已经闹过了。非常驻。
	 */
	public void notifyTimeout(Alarm alarm, long timeout)
	{
		notify(alarm.id, createTimeoutIntent(alarm, timeout));
	}
	
	
	
	/////
	private Notification createAlarmIntent(Alarm alarm)
	{
		final String title = mBrief.getAlarmTitle(alarm);
		final String message = mBrief.getAlarmBrief(alarm);
		Notification notification = new Notification();
		
		Intent intent = new Intent(mCtx, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		notification.icon = R.drawable.notification_icon;
		notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
		notification.contentIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		notification.contentView = getAlarmView(title, message);
		if(Build.VERSION.SDK_INT>=16)
		{
			notification = getPriorityNotification(notification);
		}
		return notification;
	}
	
	private Notification getPriorityNotification(Notification n)
	{
		n.priority = Notification.PRIORITY_MAX;
		return n;
	}
	
	//
	private Notification createAlertIntent(Alarm alarm)
	{
		final String title = mBrief.getAlertTitle(alarm);
		final String message = mBrief.getAlertBrief(alarm);
		Notification notification = new Notification(R.drawable.notification_icon,
				title, System.currentTimeMillis());
		notification.flags = Notification.FLAG_NO_CLEAR;
		
		Intent intent = new Intent(mCtx, AlertActivity.class);
		intent.putExtra(Cmd.Alert.EXTRA_ALARM, alarm);
		
		PendingIntent contentIntent = PendingIntent.getActivity(mCtx, 0,
				intent, PendingIntent.FLAG_CANCEL_CURRENT);
		notification.setLatestEventInfo(mCtx, title, message, contentIntent);
		return notification;
	}
	
	//
	private Notification createSnoozedIntent(Alarm alarm)
	{
		final String title = mBrief.getSnoozedTitle(alarm);
		final String message = mBrief.getSnoozedBrief(alarm);
		Notification notification = new Notification(R.drawable.notification_icon,
				title, System.currentTimeMillis());
		notification.flags = Notification.FLAG_NO_CLEAR;
		
		Intent intent = new Intent(Cmd.ACTION_ALARM_DISMISS);
		intent.putExtra(Cmd.Alert.EXTRA_ALARM, alarm);
		PendingIntent contentIntent = PendingIntent.getBroadcast(mCtx, 0,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		notification.setLatestEventInfo(mCtx, title, message, contentIntent);
		return notification;
	}
	
	private Notification createTimeoutIntent(Alarm alarm, long timeout)
	{
		final String title = mBrief.getTimeoutTitle(alarm);
		final String message = mBrief.getTimeoutBrief(alarm);
		Notification notification = new Notification(R.drawable.notification_icon,
				title, System.currentTimeMillis());
		notification.flags = Notification.FLAG_AUTO_CANCEL;

		Intent intent = new Intent(mCtx, MainActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(mCtx, 0,
				intent, Intent.FLAG_ACTIVITY_NEW_TASK);
		notification.setLatestEventInfo(mCtx, title, message, contentIntent);
		return notification;
	}
	
	private RemoteViews getAlarmView(String title, String message)
	{
		RemoteViews mviewNotification = new RemoteViews(mCtx.getPackageName(), R.layout.view_main_notification);
		mviewNotification.setTextViewText(R.id.main_notify_title, title);
		mviewNotification.setTextViewText(R.id.main_notify_message, message);    	
		mviewNotification.setImageViewResource(R.id.main_notify_icon, R.drawable.ic_launcher);
		return mviewNotification;
	}
}
